package com.wthink.newMain.settings

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.ActivitySettingsBinding
import com.wthink.newMain.settings.config.BasicConfigFragment
import com.wthink.newMain.settings.config.inner.NetConfigNewFragment
import com.wthink.newMain.settings.data.DataFragment
import com.wthink.newMain.settings.menuManager.MenuManagerFragment
import com.wthink.newMain.settings.update.UpdateFixFragment
import com.wthink.newMain.settings.version.VersionFragment
import com.wthink.newMain.settings.viewmodel.SettingsViewmodel

class SettingsActivity : AppCompatActivity(){

    lateinit var binding: ActivitySettingsBinding
    private val viewModel by viewModels<SettingsViewmodel>()

    var basicConfigFragment: BasicConfigFragment? = null
    var versionFragment: VersionFragment? =null
    var dataFragment : DataFragment ? = null
    var menuManagerFragment : MenuManagerFragment? = null
    var updateFixFragment : UpdateFixFragment? = null
    var netConfigNewFragment : NetConfigNewFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_settings)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel

        binding.back.setOnClickListener {
            finish()
        }
        initUI()
    }

    private fun initUI() {
        basicConfigFragment = BasicConfigFragment()
        versionFragment = VersionFragment()
        dataFragment  = DataFragment()
        menuManagerFragment = MenuManagerFragment()
        updateFixFragment = UpdateFixFragment()
        netConfigNewFragment = NetConfigNewFragment()

        addFragment(basicConfigFragment!!)
        binding.ivConfig.isSelected = true

        binding.ivVersion.setOnClickListener {
            addFragment(versionFragment!!)
            binding.ivVersion.isSelected = true
            binding.ivConfig.isSelected = false
            binding.ivData.isSelected = false
            binding.ivStorage.isSelected = false
            binding.ivUp.isSelected = false
            binding.ivMenuManager.isSelected = false
        }

        binding.ivConfig.setOnClickListener {
            addFragment(basicConfigFragment!!)
            binding.ivVersion.isSelected = false
            binding.ivConfig.isSelected = true
            binding.ivData.isSelected = false
            binding.ivStorage.isSelected = false
            binding.ivUp.isSelected = false
            binding.ivMenuManager.isSelected = false
        }

        binding.ivData.setOnClickListener {
            addFragment(dataFragment!!)
            binding.ivVersion.isSelected = false
            binding.ivConfig.isSelected = false
            binding.ivData.isSelected = true
            binding.ivStorage.isSelected = false
            binding.ivUp.isSelected = false
            binding.ivMenuManager.isSelected = false
        }

        binding.ivUp.setOnClickListener {
            addFragment(updateFixFragment!!)
            binding.ivVersion.isSelected = false
            binding.ivConfig.isSelected = false
            binding.ivData.isSelected = false
            binding.ivStorage.isSelected = false
            binding.ivUp.isSelected = true
            binding.ivMenuManager.isSelected = false
        }

        binding.ivMenuManager.setOnClickListener {
            addFragment(menuManagerFragment!!)
            binding.ivVersion.isSelected = false
            binding.ivConfig.isSelected = false
            binding.ivData.isSelected = false
            binding.ivStorage.isSelected = false
            binding.ivUp.isSelected = false
            binding.ivMenuManager.isSelected = true
        }

        binding.ivStorage.setOnClickListener {
            addFragment(netConfigNewFragment!!)
            binding.ivVersion.isSelected = false
            binding.ivConfig.isSelected = false
            binding.ivData.isSelected = false
            binding.ivStorage.isSelected = true
            binding.ivUp.isSelected = false
            binding.ivMenuManager.isSelected = false
        }
    }

    private fun addFragment(fragment: Fragment) {
        supportFragmentManager
            .beginTransaction()
          // .setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
            .replace(R.id.content, fragment, fragment.javaClass.simpleName)
          //.addToBackStack(fragment.javaClass.simpleName)
            .commit()
    }
}